//
//  ST_ChatToolBarAudioButton.m
//  XmppBlog
//
//  Created by DouLei on on 2017/7/16.
//  Copyright © 2017年 lanouhn. All rights reserved.
//

#import "ST_ChatToolBarAudioButton.h"
#import "ST_DPAudioRecorder.h"

@interface ST_ChatToolBarAudioButton ()
{
    BOOL isCancelSendAudioMessage;      //用户是否取消发送消息
}
@property(strong,nonatomic)UILongPressGestureRecognizer *longPress;

@property(strong,nonatomic)ST_DPAudioRecorder *recorder;

@end

@implementation ST_ChatToolBarAudioButton

-(ST_DPAudioRecorder *)recorder{
    if (!_recorder) {
        _recorder = _recorder = [ST_DPAudioRecorder sharedInstance];
        
        __weak typeof(self) weakSelf = self;
        //录音完成回调
         _recorder.audioRecorderFinishRecording = ^(NSData *data, NSUInteger audioTimeLength) {
        
             __strong typeof(self) strongSelf = weakSelf;
             
     if (strongSelf->isCancelSendAudioMessage) {
         if ([strongSelf.delegate respondsToSelector:@selector(DPAudioRecordingFail:)]) {
             [strongSelf.delegate DPAudioRecordingFail:@"用户取消发送"];
         }
    } else {
        
        if ([strongSelf.delegate respondsToSelector:@selector(DPAudioRecordingFinishWithData:withBodyString:)]) {
             
         [strongSelf.delegate DPAudioRecordingFinishWithData:data withBodyString:[NSString stringWithFormat:@"%ld", audioTimeLength/2]];
             
         }
         
         [strongSelf.longPress setEnabled:NO];
         
         [strongSelf.longPress setEnabled:YES];
    }
 };
         
         //录音开始回调
         _recorder.audioStartRecording = ^(BOOL isRecording) {
             if ([weakSelf.delegate respondsToSelector:@selector(DPAudioStartRecording:)]) {
                 [weakSelf.delegate DPAudioStartRecording:isRecording];
             }
         };
         
         //录音失败回调
         _recorder.audioRecordingFail = ^(NSString *reason) {
             if ([weakSelf.delegate respondsToSelector:@selector(DPAudioRecordingFail:)]) {
                 [weakSelf.delegate DPAudioRecordingFail:reason];
         }
        };
         
         //音频值测量回调
         _recorder.audioSpeakPower = ^(float power) {
         
             if ([weakSelf.delegate respondsToSelector:@selector(DPAudioSpeakPower:)]) {
             
             [weakSelf.delegate DPAudioSpeakPower:power];
             }
         };
    }
    return _recorder;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
        
        UIColor *color = [[UIColor alloc]initWithRed:169/255.0 green:173/255.0 blue:205/255.0 alpha:1];
        
        [self setTitleColor:color forState:UIControlStateNormal];
        [self setTitle:@"按住说话" forState:UIControlStateNormal];
        [self setTitle:@"松开结束" forState:UIControlStateHighlighted];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        
//        [self setBackgroundImage:[UIImage imageNamed:@"chatBar_recordBg"] forState:UIControlStateNormal];
//        [self setBackgroundImage:[UIImage imageNamed:@"chatBar_recordSelectedBg"] forState:UIControlStateHighlighted];
        
        self.longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
        self.longPress.minimumPressDuration = 0;
        [self addGestureRecognizer:self.longPress];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancelRecordAudio) name:UIApplicationWillResignActiveNotification object:nil];
        
    }
    return self;
}

- (void)longPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:self];
    
    if(gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        [self setTitle:@"松开 结束" forState:UIControlStateNormal];
        [self setBackgroundImage:[UIImage imageNamed:@"chatBar_recordSelectedBg"] forState:UIControlStateNormal];
        [self audioStart];
    } else if(gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        [self setTitle:@"按住 说话" forState:UIControlStateNormal];
        [self setBackgroundImage:[UIImage imageNamed:@"chatBar_recordBg"] forState:UIControlStateNormal];
        [self audioStop];
    } else if(gestureRecognizer.state == UIGestureRecognizerStateChanged) {
        if ([self.layer containsPoint:point]) {
            [self setTitle:@"松开 结束" forState:UIControlStateNormal];
            isCancelSendAudioMessage = NO;
            [self.delegate DPAudioRecording];
        } else {
            [self setTitle:@"松开 取消" forState:UIControlStateNormal];
            isCancelSendAudioMessage = YES;
            [self.delegate DPCanCancelAudioRecording];
        }
    } else if (gestureRecognizer.state == UIGestureRecognizerStateFailed) {
        NSLog(@"失败");
    } else if (gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
        NSLog(@"取消");
    }
}

- (void)audioStart
{
    //开始录音
    [self.recorder startRecording];
}

//结束录音
- (void)audioStop
{
    [self.recorder stop_recording];
}

//录音失败
- (void)audioFailed
{
    //do something
    NSLog(@"录制失败");
}

- (void)cancelRecordAudio
{
    [self.longPress setEnabled:NO];
    [self.longPress setEnabled:YES];
    
     [self setTitle:@"按住 说话" forState:UIControlStateNormal];
    [self setBackgroundImage:[UIImage imageNamed:@"chatBar_recordBg"] forState:UIControlStateNormal];
    [self audioStop];
    
}


-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
